﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WFONE
{
    public partial class Double01 : Form
    {
        public static CDeDataDl cdedata = new CDeDataDl();
        public Double01()
        {
            InitializeComponent();
            Visible = false;
            cdedata = TopWindows.cdevice.cDeDataDoubleListHead;

            while (cdedata != null)
            {
                dateNameMe.Items.Add(cdedata.datetime.ToString());
                cdedata = cdedata.next;
            }
                      
            //dateNameMe.DropDownStyle = DropDownList;
            // dateNameMe.SelectedItem = 0;
        }

        private void Double01_Load(object sender, EventArgs e)
        {

        }
        public bool inputBool()//判断数据是否完整
        {
            if (tBA11.Text == "" || tBA22.Text == "" || tBA21.Text == "" || tBA12.Text == ""
                || tBR1s.Text == ""
                || tBR1x.Text == ""
                || tBR2s.Text == ""
                || tBR2x.Text == ""
                || tBG1p.Text == ""
                || tBR1p.Text == ""
                || tBG2p.Text == ""
                || tBR2p.Text == ""
                || tBAngle1.Text == ""
                || tBAngle2.Text == ""
                )
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        private void inputData()//输入数据
        {
            try
            {
                CList.cInputListDl.dA11 = Math.Round(double.Parse(tBA11.Text), 3);
                CList.cInputListDl.dA12 = Math.Round(double.Parse(tBA12.Text), 3);
                CList.cInputListDl.dA21 = Math.Round(double.Parse(tBA21.Text), 3);
                CList.cInputListDl.dA22 = Math.Round(double.Parse(tBA22.Text), 3);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

        }
        private void dateNameMe_SelectedIndexChanged(object sender, EventArgs e)
        {
            DateTime dateTime = new DateTime();
            try
            {
                if (dateNameMe.Text != "")
                {
                    dateTime = Convert.ToDateTime(dateNameMe.Text);
                }
                if (TopWindows.cdevice != null)
                {
                    if (TopWindows.cdevice.SearchDl(dateTime) != null)
                    {
                        cdedata = TopWindows.cdevice.SearchDl(dateTime);
                        RshowInput();
                    }
                    else
                    {
                        MessageBox.Show("找不到该时间对应的设备数据");
                    }
                }
            }
            catch
            {
                MessageBox.Show("并不能找到对应的设备数据");
            }

        }
        private void RshowInput()
        {
            //cdedata.datetime = dateTime;
            if (cdedata != null)
            {
                tBR1s.Text = cdedata.dR1s.ToString();
                tBR1x.Text = cdedata.dR1x.ToString();
                tBR1p.Text = cdedata.dR1p.ToString();
                tBG1p.Text = cdedata.dG1p.ToString();
                tBAngle1.Text = cdedata.dPAngle1.ToString();

                tBR2s.Text = cdedata.dR2s.ToString();
                tBR2x.Text = cdedata.dR2x.ToString();
                tBR2p.Text = cdedata.dR2p.ToString();
                tBG2p.Text = cdedata.dG2p.ToString();
                tBAngle2.Text = cdedata.dPAngle2.ToString();
            }
        }


        private void link_to_nextWindows_MouseClick(object sender, MouseEventArgs e)
        {
            //先检测数据输入是否完整
            if (inputBool())
            {
                //如果没有选定数据库中的任何一种数据，则在该设备的设备数据链中新增一组数据
                if (dateNameMe.Text == "")
                {
                    dateNameMe.Text = DateTime.Now.ToString();
                    CDeDataDl cdedataTemp = new CDeDataDl();
                    cdedataTemp.datetime = DateTime.Now;
                    try
                    {
                        cdedataTemp.dR1s = Math.Round(double.Parse(tBR1s.Text), 3);
                        cdedataTemp.dR1x = Math.Round(double.Parse(tBR1x.Text), 3);
                        cdedataTemp.dR1p = Math.Round(double.Parse(tBR1p.Text), 3);
                        cdedataTemp.dG1p = Math.Round(double.Parse(tBG1p.Text), 3);
                        cdedataTemp.dPAngle1 = Math.Round(double.Parse(tBAngle1.Text), 3);
                        cdedataTemp.dR2s = Math.Round(double.Parse(tBR2s.Text), 3);
                        cdedataTemp.dR2x = Math.Round(double.Parse(tBR2x.Text), 3);
                        cdedataTemp.dR2p = Math.Round(double.Parse(tBR2p.Text), 3);
                        cdedataTemp.dG2p = Math.Round(double.Parse(tBG2p.Text), 3);
                        cdedataTemp.dPAngle1 = Math.Round(double.Parse(tBAngle2.Text), 3);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                    //新增一组数据,并将窗口的设备数据指针指向它
                    TopWindows.cdevice.AddDeData(cdedataTemp);
                    cdedata = cdedataTemp;
                    dateNameMe.Items.Add(cdedata.datetime.ToString());
                }
                else
                {
                    //有选择数据库中的某组数据
                    if (DataBeingChanged_Or_Not())
                    {
                        try
                        {
                            //如果数据被修改过，则更新数据
                            //dateNameMe.Focus();
                            dateNameMe.Items.Remove(cdedata.datetime.ToString());
                            cdedata.datetime = DateTime.Now;
                       
                            cdedata.dR1s = Math.Round(double.Parse(tBR1s.Text), 3);
                            cdedata.dR1x = Math.Round(double.Parse(tBR1x.Text), 3);
                            cdedata.dR1p = Math.Round(double.Parse(tBR1p.Text), 3);
                            cdedata.dG1p = Math.Round(double.Parse(tBG1p.Text), 3);
                            cdedata.dPAngle1 = Math.Round(double.Parse(tBAngle1.Text), 3);
                            cdedata.dR2s = Math.Round(double.Parse(tBR2s.Text), 3);
                            cdedata.dR2x = Math.Round(double.Parse(tBR2x.Text), 3);
                            cdedata.dR2p = Math.Round(double.Parse(tBR2p.Text), 3);
                            cdedata.dG2p = Math.Round(double.Parse(tBG2p.Text), 3);
                            cdedata.dPAngle1 = Math.Round(double.Parse(tBAngle2.Text), 3);
                            dateNameMe.Items.Add(cdedata.datetime.ToString());
                            
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }
                    }
                    inputData();
                    Double02 cDouble02 = new Double02();
                    cDouble02.Visible = false;
                    cDouble02.ShowDialog();
                }

            }
            else
            {
                MessageBox.Show("请输入完整数据", "提示信息", MessageBoxButtons.OK);

            }

        }

        //判断在窗口中预先输入的数据是否被修改
        private bool DataBeingChanged_Or_Not()
        {

            if (double.Parse(tBR1s.Text) == cdedata.dR1s
                || double.Parse(tBR1x.Text) == cdedata.dR1x
                || double.Parse(tBR2s.Text) == cdedata.dR2s
                || double.Parse(tBR2x.Text) == cdedata.dR2x
                || double.Parse(tBG1p.Text) == cdedata.dG1p
                || double.Parse(tBR1p.Text) == cdedata.dR1p
                || double.Parse(tBG2p.Text) == cdedata.dG2p
                || double.Parse(tBR2p.Text) == cdedata.dR2p
                || double.Parse(tBAngle1.Text) == cdedata.dPAngle1
                || double.Parse(tBAngle2.Text) == cdedata.dPAngle2
                )
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
        
